package com.ktgj.customer.common;

import cn.hutool.crypto.symmetric.DES;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.nacos.common.utils.MD5Utils;
import com.alibaba.nacos.shaded.org.checkerframework.checker.regex.RegexUtil;
import com.ktgj.common.core.constant.Constant;
import com.ktgj.common.core.keyutil.KeyUtils;
import com.ktgj.common.core.utils.RSAUtils;
import com.ktgj.common.core.utils.ServletUtils;
import com.ktgj.common.core.utils.xml.XElement;
import com.ktgj.common.core.utils.HttpClientUtil;
import com.ktgj.customer.domain.Insurance.axModel.JsjClaimData;
import com.ktgj.customer.domain.Insurance.dxModel.*;
import com.sun.media.jfxmedia.track.Track;
import jdk.nashorn.internal.runtime.regexp.joni.Regex;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.RegExUtils;
import org.omg.IOP.Encoding;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import javax.sound.sampled.AudioFormat;
import java.net.URLEncoder;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Component
public class DxInsuranceUtils {

    @Value("${dx-insurance.key}")
    private String key;
    @Value("${dx-insurance.url}")
    private String Url;

    @Value("${dx-insurance.key}")
    private String YuhaoDesKey;
    @Value("${dx-insurance.key}")
    private String YuhaoSignKey;
    @Value("${dx-insurance.YuhaoProductCode}")
    private String YuhaoProductCode;
    @Value("${dx-insurance.YuhaoVersion}")
    private String YuhaoVersion;
    @Value("${dx-insurance.YuhaoUserName}")
    private String YuhaoUserName;
    @Value("${dx-insurance.YuhaoCmd}")
    private String YuhaoCmd;
    @Value("${dx-insurance.YuhaoProgressCmd}")
    private String YuhaoProgressCmd;
    @Value("${dx-insurance.YuhaoClaimUrl}")
    private String YuhaoClaimUrl;
    /**
     * 大象保险开保
     * @param insureRequest 开保请求信息
     * @return
     */
    public InsureResponse openInsurance(InsureRequest insureRequest) {
        String response = postInsuranceData(insureRequest);
        InsureResponse insureResponse = JSON.parseObject(response,InsureResponse.class);
        return insureResponse;
    }

    /**
     * 查询大象保单的信息
     * @param policyStateRequest 保单的请求信息
     * @return
     */
    public GetPolicyStateResponse searchInsurance(GetPolicyStateRequest policyStateRequest)
    {
        String response = postInsuranceData(policyStateRequest);
        GetPolicyStateResponse policyStateResponse = JSON.parseObject(response,GetPolicyStateResponse.class);
        return policyStateResponse;
    }

    /**
     * 大象保险退保
     * @param refundRequset 退保请求信息
     * @return
     */
    public RefundResponse SurrenderInsurance(RefundRequset refundRequset)
    {
        String response = postInsuranceData(refundRequset);
        RefundResponse refundResponse = JSON.parseObject(response,RefundResponse.class);
        return refundResponse;
    }





    public String postInsuranceData(Object object)
    {
        String rsaKey = null;
        try {
//            String key = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCisiHg89yyyB2bN6V3ws6GRm4tJpgfMwuffOX4iV6l8XRTRP1v5BOqIBe5uDrbWRG6JX8A1HpwSixr/cSeG8hsZ++oX8m+XkvwMtr3ivLh1BNat3+8p8QqvyC2bd3vlkUg0RXinGw7vL/NcWXe49PbeT8UNe0B6tUoyrnufMiGkQIDAQAB";
//            rsaKey = RSAUtils.publicEncrypt(JSON.toJSONString(object, SerializerFeature.DisableCircularReferenceDetect,SerializerFeature.WriteDateUseDateFormat), RSAUtils.getPublicKey(key));
            rsaKey = RSAUtils.publicEncrypt(JSON.toJSONString(object, SerializerFeature.DisableCircularReferenceDetect,SerializerFeature.WriteDateUseDateFormat), RSAUtils.getPublicKey(this.key));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return "";
        } catch (InvalidKeySpecException e) {
            e.printStackTrace();
            return "";
        }
        DXRequest dxRequest = new DXRequest();
        dxRequest.setBizData(rsaKey);
//        String Url = "https://tapi.bz365.com/api/smgw.do";
//        String respose =  HttpClientUtil.doPostJson(Url,JSON.toJSONString(dxRequest));
        String respose =  HttpClientUtil.doPostJson(this.Url,JSON.toJSONString(dxRequest));
        return respose;
    }

}

